package controller.room
{
	import events.room.EventSetSelection;
	
	import flash.events.MouseEvent;
	
	import model.ModelSelection;
	
	import org.flashIso.engine.base.IsoMap;
	import org.flashIso.engine.core.Point2D;
	import org.flashIso.engine.core.Point3D;
	import org.flashIso.engine.objects.IsoObject;
	import org.flashIso.engine.objects.IsoTile;
	import org.robotlegs.mvcs.Command;
	
	public class CommandSetSelection extends Command
	{
		[Inject]
		public var e:EventSetSelection;
		
		[Inject]
		public var ms:ModelSelection;
		
		override public function execute() : void {
			removeSelectedFilter();
			var i:uint;
			if (e.tiles != null){
				ms.selectedTiles.removeAll();
				for (i = 0; i < e.tiles.length; i++){
					ms.selectedTiles.addItem(e.tiles[i]);
				}
			}
			if (e.addTiles != null){
				for (i = 0; i < e.addTiles.length; i++){
					ms.selectedTiles.addItem(e.addTiles[i]);
				}
			}
			if (e.removeTiles != null){
				for (i = 0; i < e.removeTiles.length; i++){
					ms.selectedTiles.removeItemAt(ms.selectedTiles.getItemIndex(e.removeTiles[i]));
				}
			}
			if (e.objects != null){
				ms.selectedObjects.removeAll();
				for (i = 0; i < e.objects.length; i++){
					ms.selectedObjects.addItem(e.objects[i]);
				}
			}
			if (e.addObjects != null){
				for (i = 0; i < e.addObjects.length; i++){
					ms.selectedObjects.addItem(e.addObjects[i]);
				}
			}
			if (e.removeObjects != null){
				for (i = 0; i < e.removeObjects.length; i++){
					ms.selectedObjects.removeItemAt(ms.selectedObjects.getItemIndex(e.removeObjects[i]));
				}
			}
			setSelectedFilter();
		}
		
		private function removeSelectedFilter() : void {
			var i:uint;
			for (i = 0; i < ms.selectedObjects.length; i++){
				ms.selectedObjects[i].filters = [];
			}
			for (i = 0; i < ms.selectedTiles.length; i++){
				ms.selectedTiles[i].filters = [];
			}
		}
		private function setSelectedFilter() : void {
			var i:uint;
			for (i = 0; i < ms.selectedObjects.length; i++){
				ms.selectedObjects[i].filters = ms.selectionFilters;
			}
			for (i = 0; i < ms.selectedTiles.length; i++){
				ms.selectedTiles[i].filters = ms.selectionFilters;
			}
		}
	}
}